//
//  TimeUtils.m
//  woyaopao
//

#import "TimeUtils.h"

@implementation TimeUtils

+ (NSDateFormatter*)getFormat1 {
    return [self getFormat:@"yyyy年MM月dd日"];
}

+ (NSDateFormatter*)getFormat2 {
    return [self getFormat:@"yyyy-MM-dd HH:mm:ss"];
}

+ (NSDateFormatter*)getFormat3 {
    return [self getFormat:@"MM月dd日"];
}

+ (NSDateFormatter*)getFormat4 {
    return [self getFormat:@"yyyy-MM-dd HH:mm"];
}

+ (NSDateFormatter*)getFormat5 {
    return [self getFormat:@"yyyy-MM-dd"];
}

+ (NSDateFormatter*)getFormat6 {
    return [self getFormat:@"yyyy-MM"];
}

+ (NSDateFormatter*)getFormat7 {
    return [self getFormat:@"HH:mm"];
}

+ (NSDateFormatter*)getFormat8 {
    return [self getFormat:@"yyyy-MM-dd（EEE） HH:mm"];
}

+ (NSDateFormatter*)getFormat9 {
    return [self getFormat:@"yyyy-MM-dd EEEE"];
}

+ (NSDateFormatter*)getFormat10 {
    return [self getFormat:@"yyyy-MM-dd（EEE） HH:mm:ss"];
}

+ (NSDateFormatter*)getFormat11 {
    return [self getFormat:@"yyyy年MM月"];
}

+ (NSDateFormatter*)getFormat12 {
    return [self getFormat:@"yyyyMMddHHmmss"];
}

+ (NSDateFormatter*)getFormat:(NSString *)formatterString {
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    format.dateFormat = formatterString;
    return format;
}

+ (long long)getCurTimestamp {
    return (long long)([[NSDate date] timeIntervalSince1970]*1000);
}

+ (long long)getTimestamp:(NSDate *)date {
    return (long long)([date timeIntervalSince1970]*1000);
}

+ (NSString *)getCurTime {
    return [[self getFormat2]stringFromDate:[NSDate date]];
}

+ (NSTimeInterval)intervalFromLastDate:(NSString *)dateString1  toTheDate:(NSString *)dateString2 {
    if (dateString1.length == 10) {
        dateString1 = [NSString stringWithFormat:@"%@ 00:00",dateString1];
    }
    if (dateString2.length == 10) {
        dateString2 = [NSString stringWithFormat:@"%@ 00:00",dateString2];
    }
    NSDate *theDate = [[self getFormat4] dateFromString:dateString2];
    NSDate *lastDate = [[self getFormat4] dateFromString:dateString1];
    NSTimeInterval different = [lastDate timeIntervalSinceDate:theDate];
    return different;
}


+ (NSString *)prettyDateWithReference:(NSString *)reference {
    NSString *suffix = @"前";
    NSDate *referenceDate = [[self getFormat2] dateFromString:reference];
    float different = [[NSDate date] timeIntervalSinceDate:referenceDate];
    if (different < 0) {
        different = -different;
        suffix = @"后";
    }
    
    // days = different / (24 * 60 * 60), take the floor value
    float dayDifferent = floor(different / 86400);
    
//    int days   = (int)dayDifferent;
//    int weeks  = (int)ceil(dayDifferent / 7);
//    int months = (int)ceil(dayDifferent / 30);
//    int years  = (int)ceil(dayDifferent / 365);
    
    // It belongs to today
    if (dayDifferent <= 0) {
        // lower than 60 seconds
        if (different < 60) {
            return @"刚刚";
        }
        
        // lower than 120 seconds => one minute and lower than 60 seconds
        if (different < 120) {
            return [NSString stringWithFormat:@"1分钟%@", suffix];
        }
        
        // lower than 60 minutes
        if (different < 60 * 60) {
            return [NSString stringWithFormat:@"%d分钟%@", (int)floor(different / 60), suffix];
        }
        
        // lower than 60 * 2 minutes => one hour and lower than 60 minutes
        if (different < 7200) {
            return [NSString stringWithFormat:@"1小时%@", suffix];
        }
        
        // lower than one day
        if (different < 86400) {
            return [NSString stringWithFormat:@"%d小时%@", (int)floor(different / 3600), suffix];
        }
    }
    else {
        return [reference substringToIndex:10];
    }
//    // lower than one week
//    else if (days < 7) {
//        return [NSString stringWithFormat:@"%d天%@", days, suffix];
//    }
//    // lager than one week but lower than a month
//    else if (weeks < 4) {
//        return [NSString stringWithFormat:@"%d周%@", weeks, suffix];
//    }
//    // lager than a month and lower than a year
//    else if (months < 12) {
//        return [NSString stringWithFormat:@"%d月%@", months, suffix];
//    }
//    // lager than a year
//    else {
//        return [NSString stringWithFormat:@"%d年%@", years, suffix];
//    }
    
    return self.description;
}

@end
